package com.ty.skysystem.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.skysystem.dao.ContactDao;
import com.ty.skysystem.dao.TruckDao;
import com.ty.skysystem.entity.ContactEntity;
import com.ty.skysystem.entity.TruckEntity;
import com.ty.skysystem.entity.req.DriverEntityREQ;
import com.ty.skysystem.entity.req.TruckEntityREQ;
import com.ty.skysystem.entity.vo.TruckEntityVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.Query;

import com.ty.skysystem.dao.DriverDao;
import com.ty.skysystem.entity.DriverEntity;
import com.ty.skysystem.service.DriverService;

@Service("driverService")
@Slf4j
public class DriverServiceImpl extends ServiceImpl<DriverDao, DriverEntity>
    implements DriverService {

  @Autowired private ContactDao contactDao;

  @Autowired private TruckDao truckDao;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<DriverEntity> page =
        this.page(
            new Query<DriverEntity>().getPage(params),
            new QueryWrapper<DriverEntity>()
                .select(DriverEntity.class, e -> !"is_deleted".equals(e.getColumn())));

    return new PageUtils(page);
  }

  /**
   * 查询未绑定车辆的驾驶员
   *
   * @return
   */
  @Override
  public PageUtils selectDriversWithoutBoundVehicles(
      Integer page, Integer limit, DriverEntityREQ driverEntityREQ) {

    IPage<DriverEntity> driverEntityIPage =
        baseMapper.selectDriversUnboundTruck(new Page<>(page, limit), driverEntityREQ);

    return new PageUtils(driverEntityIPage);
  }

  /**
   * 查询未绑定驾驶员的车辆
   *
   * @return
   */
  @Override
  public PageUtils selectVehicleWithoutDriverBound(
      Integer page, Integer limit, TruckEntityREQ truckEntityREQ) {
    IPage<TruckEntity> truckEntityIPage =
        this.truckDao.selectTruckUnboundDriver(new Page<>(page, limit), truckEntityREQ);

    return new PageUtils(truckEntityIPage);
  }
  /**
   * 根据驾驶员ID查出已经绑定的多个车辆
   */
  @Override
  public List<TruckEntityVo> selectTruckIsDriverId(Integer driverId) {
    return this.baseMapper.selectTruckIsDriverId(driverId);
  }
}
